//
//  ImagePrintHelp.m
//  Common
//
//  Created by xiong qi on 13-4-26.
//  Copyright (c) 2013年 xiong qi. All rights reserved.
//

#import "ImagePrintHelp.h"
#import <QuartzCore/QuartzCore.h>

@implementation ImagePrintHelp
static ImagePrintHelp * _instance;

+(ImagePrintHelp*)getInstance
{
    @synchronized(self)
    {
        if(_instance == nil)
            _instance = [ImagePrintHelp new];
    }
    return _instance;
}

-(id)init
{
    self =[super init];
    if (self) {
        
    }
    return self;
}

-(UIImage *)GetViewThumbnailImage:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];
	
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
	
    UIGraphicsEndImageContext();
    return img;
}

-(BOOL)PrintImage:(UIImage *)image
{
    UIPrintInteractionController *print = [UIPrintInteractionController sharedPrintController];
    
    if(!print){
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Print Unavailable!" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = @"PrintImage";
    printInfo.duplex = UIPrintInfoDuplexLongEdge;
    print.printInfo = printInfo;
    print.showsPageRange = YES;
    print.printingItem = image;
    
    UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) {
        if(!completed && error){
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Print Unavailable!" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
    };
    
    [print presentAnimated:YES completionHandler:completionHandler];
    
    return NO;
}

@end
